import { Space, Divider, Modal, message } from 'antd'
import { ExclamationCircleOutlined } from '@ant-design/icons'
import { ColumnsType } from 'antd/es/table'
import { DataSchema, delRequest } from './ajax'
import { awaitWrap } from '@js/tool'

const { confirm } = Modal

function confrimDel (id: string, refreshData: () => void): void {
  confirm({
    icon: <ExclamationCircleOutlined />,
    content: '您确定要删除吗？',
    onOk () {
      delHandle(id, refreshData).catch(e => console.error(e))
    }
  })
}

async function delHandle (id: string, refreshData: () => void): Promise<void> {
  const [e] = await awaitWrap(delRequest(id))
  if (e === null) {
    message.success('删除成功').then(null, null)
    refreshData()
  }
}

export function getColumn (setVisible: (v: boolean) => void, setModifyData: (v: DataSchema) => void, refreshData: () => void): ColumnsType<DataSchema> {
  const columns: ColumnsType<DataSchema> = [
    {
      title: '序号',
      dataIndex: 'orderno',
      key: 'orderno',
      width: 80
    },
    {
      title: '职责描述',
      dataIndex: 'content',
      key: 'content'
    },
    {
      title: '权重',
      dataIndex: 'weights',
      key: 'weights',
      render: (v: number) => {
        if (typeof v === 'number') {
          return v.toFixed(2)
        }
      },
      width: 100
    },
    {
      title: '自我评价',
      dataIndex: 'self_evaluation_score',
      key: 'self_evaluation_score',
      render: (v: number) => {
        if (typeof v === 'number') {
          return v.toFixed(2)
        }
      },
      width: 100
    },
    {
      title: '直接上级评价',
      dataIndex: 'leadership_score',
      key: 'leadership_score',
      render: (v: number) => {
        if (typeof v === 'number') {
          return v.toFixed(2)
        }
      },
      width: 160
    },
    {
      title: '操作',
      dataIndex: 'id',
      key: 'id',
      render: (v: string, l: DataSchema) => {
        return (
          <Space split={<Divider type="vertical" />}>
            <a onClick={() => { setModifyData(l); setVisible(true) }}>修改</a>
            <a onClick={() => { confrimDel(v, refreshData) }}>删除</a>
          </Space>
        )
      },
      width: 160
    }
  ]
  return columns
}
